﻿using System;

namespace HetmansProblemConsole
{
    class MainHetmans
    {
        private static bool[] diagonalRight;
        private static bool[] diagonalLeft;
        private static bool[] column;
        private static bool[,] board;
        private static int boardSize;
        private static string[] columnName;
        public MainHetmans()
        {
            boardSize = 8;
            diagonalRight = new bool[boardSize + 2 * (boardSize - 1)];
            diagonalLeft = new bool[boardSize + 2 * (boardSize - 1)];
            board = new bool[boardSize, boardSize];
            column = new bool[boardSize];
            columnName = new string[] { "A", "B", "C", "D", "E", "F", "G", "H" };
        }
        public void PutHetman(int row)
        {
            WriteBoard();
            for (int i = 0; i < boardSize; i++)//sprawdzamy dany wiersz
            {
                Console.WriteLine("Kolumna: " + columnName[i] + " wiersz: " + (row + 1));
                if (!((column[i]) || (diagonalRight[i - row + (boardSize - 1)]) || (diagonalLeft[row + i + (boardSize - 1)])))
                {
                    diagonalRight[i - row + (boardSize - 1)] = true;    //prawa przekątna zajęta
                    diagonalLeft[row + i + (boardSize - 1)] = true; //lewa przekątna zajęta
                    column[i] = true;               //kolumna zajęta
                    board[i, row] = true;        //dane pole na planszy zajęte

                    if (row < boardSize - 1)
                    {
                        PutHetman(row + 1);      // sprawdzamy kolejny wiersz
                    }
                    else
                    {                       // ostatni hetman ustawiony więc...
                        WriteBoard();
                        Console.WriteLine("Naciśnij przycisk aby zakończyć program ");
                        Console.ReadKey();
                        Environment.Exit(0); //koniec programu
                    }

                    //jeśli w danym wierszy nie możn postawic to usun hetmana ,ustaw kolumne i przekątne jako nie zajęte
                    diagonalRight[i - row + (boardSize - 1)] = false;
                    diagonalLeft[row + i + (boardSize - 1)] = false;
                    column[i] = false;
                    board[i, row] = false;
                }
            }
        }
        public static void WriteBoard()
        {
            Console.WriteLine("  A B C D E F G H");
            for (int y = boardSize - 1; y >= 0; y--)
            {
                Console.Write(y + 1 + " ");
                for (int x = 0; x < boardSize; x++)
                {
                    if (board[x, y])
                    {
                        Console.Write("H ");
                    }
                    else
                    {
                        Console.Write("- ");
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("------------------");
            Console.WriteLine("Sprawdzam: ");
           // Console.ReadKey();
        }
    }
}
